.TH std::basic_filebuf::swap 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::basic_filebuf::swap \- std::basic_filebuf::swap

.SH Synopsis
   void swap( std::basic_filebuf& rhs );  \fI(since C++11)\fP

   Swaps the state and the contents of *this and rhs.

.SH Parameters

   rhs - another basic_filebuf

.SH Return value

   \fI(none)\fP

.SH Notes

   This function is called automatically when swapping std::fstream objects, it is
   rarely necessary to call it directly.

.SH Example


// Run this code

 #include <fstream>
 #include <iostream>
 #include <string>

 int main()
 {
     std::ifstream fin("test.in"); // read-only
     std::ofstream fout("test.out"); // write-only

     std::string s;
     getline(fin, s);
     std::cout << s << '\\n'; // outputs the first line of test.in

     fin.rdbuf()->swap(*fout.rdbuf()); //swap the underlying buffers

     getline(fin, s); // fails: cannot read from a write-only filebuf
     std::cout << s << '\\n'; // prints empty line
 }

.SH See also

   operator=                     assigns a basic_filebuf object
   \fI(C++11)\fP                       \fI(public member function)\fP
   std::swap(std::basic_filebuf) specializes the std::swap algorithm
   \fI(C++11)\fP                       \fI(function template)\fP
   swap                          swaps two file streams
   \fI(C++11)\fP                       \fI\fI(public member\fP function of\fP
                                 std::basic_fstream<CharT,Traits>)
